from flask import session, app, request, redirect, url_for, Flask

app = Flask(__name__)


@app.route('/')
def index():
    if 'username' in session:
        username = session['username']
        return (f"Logged in as {username}"
                f"<b><a href = '/logout'>click here to log out</a></b>")
    return "You are not logged in <a href = '/login'></b> click here to log in</b></a>"


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    return '''

   <form action = "" method = "post">
      <p><input type="text" name="username"/></p>
      <p><input type="submit" value="Login"/></p>
   </form>

   '''


@app.route('/logout')
def logout():
    # remove the username from the session if it is there
    session.pop('username', None)
    return redirect(url_for('index'))


if __name__ == '__main__':
    app.secret_key = 'fkdjsafjdkfdlkjfadskjfadskljdsfklj'
    app.run(debug=True)
